﻿using UnityEngine;
using UnityEngine.UI;

namespace Jinndev {

    public static class GridViewUtil  {
        
        /// <summary>
        /// 根据容器大小，设置单元的大小到合适的值
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="roundMethod"></param>
        public static void FitGridViewCellSizeToParent(GridView gridView, RoundMethod roundMethod = RoundMethod.Round) {
            RectTransform viewport = gridView.GetComponent<ScrollRect>().viewport;
            Vector2 viewSize = viewport.rect.size;
            Vector2 contentSize = viewSize - new Vector2(gridView.padding.horizontal, gridView.padding.vertical);

            Vector2 cellSize = gridView.cellSize;
            if (gridView.constraint == GridLayoutGroup.Constraint.FixedColumnCount) {
                int col = gridView.constraintCount;
                cellSize.x = (contentSize.x - gridView.spacing.x * (col - 1)) / col;
            }
            else if (gridView.constraint == GridLayoutGroup.Constraint.FixedRowCount) {
                int row = gridView.constraintCount;
                cellSize.y = (contentSize.y - gridView.spacing.y * (row - 1)) / row;
            }
            else {
                if(gridView.startAxis == GridLayoutGroup.Axis.Horizontal) {
                    int col = Math.Round((contentSize.x - gridView.spacing.x) / (cellSize.x + gridView.spacing.x), roundMethod);
                    col = Mathf.Max(col, 1);
                    cellSize.x = (contentSize.x - gridView.spacing.x * (col - 1)) / col;
                }
                else {
                    int row = Math.Round((contentSize.y - gridView.spacing.y) / (cellSize.y + gridView.spacing.y), roundMethod);
                    row = Mathf.Max(row, 1);
                    cellSize.y = (contentSize.y - gridView.spacing.y * (row - 1)) / row;
                }
            }
            //Debug.Log(gridView.cellSize + " : " + cellSize);
            gridView.cellSize = cellSize;
        }

        /// <summary>
        /// 根据容量，自动设置容器大小
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="capacity"></param>
        /// <param name="maxWidth"></param>
        /// <param name="maxHeight"></param>
        public static void FitGridViewSize(GridView gridView, int capacity, float maxWidth, float maxHeight) {
            RectTransform viewport = gridView.GetComponent<ScrollRect>().viewport;
            RectTransform rectTransform = gridView.transform as RectTransform;

            int row;
            int col;
            if (gridView.constraint == GridLayoutGroup.Constraint.FixedColumnCount) {
                col = gridView.constraintCount;
                row = Mathf.CeilToInt((float)capacity / col);
            }
            else if (gridView.constraint == GridLayoutGroup.Constraint.FixedRowCount) {
                row = gridView.constraintCount;
                col = Mathf.CeilToInt((float)capacity / row);
            }
            else {
                return;
            }
            float width = col * gridView.cellSize.x + (col - 1) * gridView.spacing.x + gridView.padding.horizontal + (viewport.offsetMax.x - viewport.offsetMin.x);
            float height = row * gridView.cellSize.y + (row - 1) * gridView.spacing.y + gridView.padding.vertical + (viewport.offsetMax.y - viewport.offsetMin.y);
            if (maxWidth > 0) {
                width = Mathf.Min(width, maxWidth);
            }
            if (maxHeight > 0) {
                height = Mathf.Min(height, maxHeight);
            }
            rectTransform.sizeDelta = new Vector2(width, height);
        }

        /// <summary>
        /// 根据容量，自动设置容器大小
        /// </summary>
        /// <param name="gridView"></param>
        /// <param name="capacity"></param>
        /// <param name="maxWidth"></param>
        /// <param name="maxHeight"></param>
        public static void FitGridViewSize(GridView gridView, int capacity, int maxRow, int maxCol) {
            RectTransform viewport = gridView.GetComponent<ScrollRect>().viewport;
            RectTransform rectTransform = gridView.transform as RectTransform;

            int row;
            int col;
            if (gridView.constraint == GridLayoutGroup.Constraint.FixedColumnCount) {
                col = gridView.constraintCount;
                row = Mathf.CeilToInt((float)capacity / col);
            }
            else if (gridView.constraint == GridLayoutGroup.Constraint.FixedRowCount) {
                row = gridView.constraintCount;
                col = Mathf.CeilToInt((float)capacity / row);
            }
            else {
                return;
            }
            if (maxRow > 0) {
                row = Mathf.Min(row, maxRow);
            }
            if (maxCol > 0) {
                col = Mathf.Min(col, maxCol);
            }
            float width = col * gridView.cellSize.x + (col - 1) * gridView.spacing.x + gridView.padding.horizontal + (viewport.offsetMax.x - viewport.offsetMin.x);
            float height = row * gridView.cellSize.y + (row - 1) * gridView.spacing.y + gridView.padding.vertical + (viewport.offsetMax.y - viewport.offsetMin.y);
            rectTransform.sizeDelta = new Vector2(width, height);
        }

    }


}